package com.example.ojmail.controller;

import cn.dev33.satoken.util.SaResult;
import com.alibaba.fastjson.JSONObject;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@CrossOrigin
public class MailController {

    @Resource
    private JavaMailSender mailSender;

    @PostMapping("sendMessage")
    public SaResult sendMessage(@RequestBody JSONObject body)
    {
        String to = body.getString("to");
        String text = body.getString("text");
        String subject =body.getString("subject");
        SimpleMailMessage message=new SimpleMailMessage();
        message.setFrom("cug@zzjtpp.top");
        message.setTo(to);
        message.setSubject(subject);
        message.setText(text);
        mailSender.send(message);
        return SaResult.ok();
    }
}
